﻿using System;
using System.Windows;
using Tomers.WPF.Localization;

namespace XmlVisualizer
{
    /// <summary>
    /// Provides convenience methods to display message boxes.
    /// </summary>
    public static class MessageBoxes
    {
        /// <summary>
        /// Application's name.
        /// </summary>
        public static string ApplicationName = XmlVisualizer.Resources.Strings.ApplicationName;

        /// <summary>
        /// Displays an error dialog with a given message.
        /// </summary>
        /// <param name="message">The message to be displayed.</param>
        public static void ShowError(string message)
        {
            ShowMessage(TranslateMessageBoxText(message), MessageBoxImage.Error);
        }

        /// <summary>
        /// Displays an error dialog with a given message.
        /// </summary>
        /// <param name="message">The message to be displayed.</param>
        public static void ShowInformation(string message)
        {
            ShowMessage(TranslateMessageBoxText(message), MessageBoxImage.Information);
        }

        /// <summary>
        /// Displays an error dialog with a given message.
        /// </summary>
        /// <param name="message">The message to be displayed.</param>
        public static void ShowWarning(string message)
        {
            ShowMessage(TranslateMessageBoxText(message), MessageBoxImage.Warning);
        }

        /// <summary>
        /// Displays an error dialog with a given message and icon.
        /// </summary>
        /// <param name="message">The message to be displayed.</param>
        /// <param name="icon">The icon to be displayed with the message.</param>
        private static void ShowMessage(string message, MessageBoxImage icon)
        {
            string appName = ApplicationName;
            MessageBox.Show(message, appName, MessageBoxButton.OK, icon);
        }

        /// <summary>
        /// Displays an OK / Cancel dialog and returns the user input.
        /// </summary>
        /// <param name="message">The message to be displayed.</param>
        /// <param name="icon">The icon to be displayed.</param>
        /// <returns>User selection.</returns>
        public static MessageBoxResult ShowOkCancel(string message, MessageBoxImage icon)
        {
            string appName = ApplicationName;
            return MessageBox.Show(TranslateMessageBoxText(message), appName, MessageBoxButton.OKCancel, icon);
        }

        /// <summary>
        /// Displays a Yes/No dialog and returns the user input.
        /// </summary>
        /// <param name="message">The message to be displayed.</param>
        /// <param name="icon">The icon to be displayed.</param>
        /// <returns>User selection.</returns>
        public static MessageBoxResult ShowYesNo(string message, MessageBoxImage icon)
        {
            string appName = ApplicationName;
            return MessageBox.Show(TranslateMessageBoxText(message), appName, MessageBoxButton.YesNo, icon);
        }

        /// <summary>
        /// Displays an Yes / No / Cancel dialog and returns the user input.
        /// </summary>
        /// <param name="message">The message to be displayed.</param>
        /// <param name="icon">The icon to be displayed.</param>
        /// <returns>User selection.</returns>
        public static MessageBoxResult ShowYesNoCancel(string message, MessageBoxImage icon)
        {
            string appName = ApplicationName;
            return MessageBox.Show(TranslateMessageBoxText(message), appName, MessageBoxButton.YesNoCancel, icon);
        }

        private static string TranslateMessageBoxText(string uid)
        {
            string s = (string)LanguageDictionary.Current.Translate(uid, "Text", null, typeof(string));
            if (String.IsNullOrEmpty(s))
                return uid;
            else
                return s;
        }
    }
}